<?php
namespace app\common\model;

use think\Model;

class Deal extends BaseModel   //model层 数据添加到数据库
{

public function getDealByStatusBisid($status=0, $bis_id)
    {
       $order = [
	       'start_time' => 'desc',
	   
	   ];
	   $data = [
	       'status' => $status,
		   'bis_id' => $bis_id,
	   ];
	   return $this->where($data)->order($order)->paginate(10);
	   
	   
	   
	}

public function getDealsByStatus($data=[], $status = 1)
    {
	   $order = [
	       'id' => 'desc',
	   
	   ];
	   $data['status']= $status;
	   $res = $this->where($data)->order($order)->paginate(2);
	   // echo $this->getLastSql();//exit;
	   return $res;
	   
	   
	   
	}

//根据分类和城市  获取商品数据
// catid 分类id  cityid 城市id    limit 展示条数    
public function getNormalDealByCategoryCityId($catid, $cityid, $limit=10){
	$data = [
	    'end_time' => ['gt', time()],
		'category_id' => $catid,
		'city_id' => $cityid,
		'status' => 1,	
	];
	$order = [
	       'listorder' => 'desc',
		   'id' => 'desc',	   
	   ];
	$res = $this->where($data)->order($order);
	   // echo $this->getLastSql();//exit;
	   if($limit){
		  $res = $res->limit($limit);
		   }
	   
	   
	   return $res->select();
	
	
	}

public function getDealByConditions($data=[], $orders){
	if(!empty($orders['order_sales'])){
		$order['buy_count'] = 'desc';
		
		}
	if(!empty($orders['order_price'])){
		$order['current_price'] = 'desc';
		
		}
	if(!empty($orders['order_time'])){
		$order['create_time'] = 'desc';
		
		}
	//find_in_set  in/not in  like
	$order['id'] = 'desc';
	$datas[] = "end_time > ".time();
	if(!empty($data['se_category_id'])){
		$datas[] =  " find_in_set(".$data['se_category_id'].", se_category_id)" ;
		
		}
	if(!empty($data['category_id'])){
		$datas[] = 'category_id = '.$data['category_id'];
		}
	if(!empty($data['city_id'])){
		$datas[] = 'city_id = '.$data['city_id'];
		}
	$datas[] = 'status = 1';
	
	//var_dump($datas);
	//$datass = implode('AND', $datas);
	//var_dump($datass);
	
	$res = $this->where(implode(' AND ', $datas))->order($order)->paginate(10);
	//echo $this->getLastSql();exit;
	return $res;
	}

public function updateBuyCountById($id, $buyCount){
	return $this->where(['id'=>$id], $this->setInc('buy_count', $buyCount));
	
	}











}